package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseFeeMapper;
import com.ly.heart.service.IKingBaseFeeService;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseFeeEntity;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-财务模块-费用项目Service业务层处理
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingBaseFeeServiceImpl extends ServiceImpl<KingBaseFeeMapper, KingBaseFeeEntity> implements IKingBaseFeeService {

    /**
     * 查询金蝶-财务模块-费用项目
     * @param id 金蝶-财务模块-费用项目ID
     * @return 金蝶-财务模块-费用项目
     */
    @Override
    public KingBaseFeeEntity selectKingBaseFeeEntityById (String id) {
        return this.getById ( id );
    }

    /**
     * 查询金蝶-财务模块-费用项目列表
     * @param kingBaseFeeEntity 金蝶-财务模块-费用项目
     * @return 金蝶-财务模块-费用项目
     */
    @Override
    public Page selectKingBaseFeeEntityList (KingBaseFeeEntity kingBaseFeeEntity) {
        Page page = this.page ( new Page<> ( kingBaseFeeEntity.getPage (), kingBaseFeeEntity.getLimit () ), new LambdaQueryWrapper<KingBaseFeeEntity> ().eq ( KingBaseFeeEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        return page;
    }

    /**
     * 新增金蝶-财务模块-费用项目
     * @param kingBaseFeeEntity 金蝶-财务模块-费用项目
     * @return 结果
     */
    @Override
    public AjaxResult insertKingBaseFeeEntity (KingBaseFeeEntity kingBaseFeeEntity) {
        kingBaseFeeEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingBaseFeeEntity.setFeeCode ( "FYXM".concat ( String.format ( "%08d", count ) ) );
        kingBaseFeeEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        kingBaseFeeEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseFeeEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingBaseFeeEntity );
        if ( ! save ) {
            throw new CustomException ( "新增费用项目失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 修改金蝶-财务模块-费用项目
     * @param kingBaseFeeEntity 金蝶-财务模块-费用项目
     * @return 结果
     */
    @Override
    public AjaxResult updateKingBaseFeeEntity (KingBaseFeeEntity kingBaseFeeEntity) {
        kingBaseFeeEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseFeeEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingBaseFeeEntity );
        if ( ! b ) {
            throw new CustomException ( "修改费用项目失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 批量删除金蝶-财务模块-费用项目
     * @param ids 需要删除的金蝶-财务模块-费用项目ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseFeeEntityByIds (String[] ids) {
        List<KingBaseFeeEntity> list = this.list (new LambdaQueryWrapper<KingBaseFeeEntity> ().in ( KingBaseFeeEntity::getId, ids ));
        List<String> idList = list.stream ().filter ( fee -> fee.getIsDel () == 1 ).map ( KingBaseFeeEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( fee -> {
            if ( fee.getIsDel () == 2 ) {
                stringBuilder.append ( fee.getTitle () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseFeeEntity> ().
                set ( KingBaseFeeEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseFeeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseFeeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseFeeEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除费用项目失败！" );
        }
        String str = "删除费用项目成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块-费用项目信息
     * @param id 金蝶-财务模块-费用项目ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled (String id) {
        KingBaseFeeEntity kingBaseFeeEntity = this.getById ( id );
        if ( kingBaseFeeEntity == null ) {
            throw new CustomException ( "无此费用项目！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseFeeEntity> ().
                set ( KingBaseFeeEntity :: getIsDisabled, kingBaseFeeEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseFeeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseFeeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseFeeEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( kingBaseFeeEntity.getIsDisabled () == 1 ? "禁用费用项目失败！" : "解禁费用项目失败！" );
        }
        return AjaxResult.success ();
    }

}
